package com.hanserwei.han_picture.controller;

import com.hanserwei.han_picture.annotation.AuthCheck;
import com.hanserwei.han_picture.constant.UserConstant;
import com.hanserwei.han_picture.domain.common.ApiResponse;
import com.hanserwei.han_picture.domain.entity.po.Space;
import com.hanserwei.han_picture.domain.entity.po.User;
import com.hanserwei.han_picture.domain.entity.vo.*;
import com.hanserwei.han_picture.domain.request.space.*;
import com.hanserwei.han_picture.enums.ResponseCodeEnum;
import com.hanserwei.han_picture.service.SpaceAnalyzeService;
import com.hanserwei.han_picture.service.UserService;
import com.hanserwei.han_picture.utils.ThrowUtils;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.servlet.http.HttpServletRequest;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@RequestMapping("/spaceAnalyze")
@RequiredArgsConstructor
@Tag(name = "SpaceAnalyzeController")
public class SpaceAnalyzeController {

    private final UserService userService;
    private final SpaceAnalyzeService spaceAnalyzeService;

    @Operation(summary = "获取空间使用情况")
    @PostMapping("/usage")
    public ApiResponse<SpaceUsageAnalyzeResponse> getSpaceUsageAnalyze(@RequestBody SpaceUsageAnalyzeRequest analyzeRequest, HttpServletRequest request) {
        ThrowUtils.throwIf(analyzeRequest == null, ResponseCodeEnum.INVALID_PARAMETER);
        User loginUser = userService.getLoginUser(request);
        SpaceUsageAnalyzeResponse response = spaceAnalyzeService.getSpaceUsageAnalyze(analyzeRequest, loginUser);
        return ApiResponse.success("获取空间使用情况成功！", response);
    }

    @Operation(summary = "获取空间图片分类情况")
    @PostMapping("/category")
    public ApiResponse<List<SpaceCategoryAnalyzeResponse>> getSpaceCategoryAnalyze(@RequestBody SpaceCategoryAnalyzeRequest analyzeRequest, HttpServletRequest request) {
        ThrowUtils.throwIf(analyzeRequest == null, ResponseCodeEnum.INVALID_PARAMETER);
        User loginUser = userService.getLoginUser(request);
        List<SpaceCategoryAnalyzeResponse> response = spaceAnalyzeService.getSpaceCategoryAnalyze(analyzeRequest, loginUser);
        return ApiResponse.success("获取空间图片分类情况成功！", response);
    }

    @Operation(summary = "获取空间图片标签情况")
    @PostMapping("/tag")
    public ApiResponse<List<SpaceTagAnalyzeResponse>> getSpaceTagAnalyze(@RequestBody SpaceTagAnalyzeRequest analyzeRequest, HttpServletRequest request) {
        ThrowUtils.throwIf(analyzeRequest == null, ResponseCodeEnum.INVALID_PARAMETER);
        User loginUser = userService.getLoginUser(request);
        List<SpaceTagAnalyzeResponse> response = spaceAnalyzeService.getSpaceTagAnalyze(analyzeRequest, loginUser);
        return ApiResponse.success("获取空间图片标签情况成功！", response);
    }

    @Operation(summary = "获取空间图片大小情况")
    @PostMapping("/size")
    public ApiResponse<List<SpaceSizeAnalyzeResponse>> getSpaceSizeAnalyze(@RequestBody SpaceSizeAnalyzeRequest analyzeRequest, HttpServletRequest request) {
        ThrowUtils.throwIf(analyzeRequest == null, ResponseCodeEnum.INVALID_PARAMETER);
        User loginUser = userService.getLoginUser(request);
        List<SpaceSizeAnalyzeResponse> response = spaceAnalyzeService.getSpaceSizeAnalyze(analyzeRequest, loginUser);
        return ApiResponse.success("获取空间图片大小情况成功！", response);
    }

    @Operation(summary = "用户上传行为分析请求")
    @PostMapping("/user")
    public ApiResponse<List<SpaceUserAnalyzeResponse>> getSpaceUserAnalyze(@RequestBody SpaceUserAnalyzeRequest analyzeRequest, HttpServletRequest request) {
        ThrowUtils.throwIf(analyzeRequest == null, ResponseCodeEnum.INVALID_PARAMETER);
        User loginUser = userService.getLoginUser(request);
        List<SpaceUserAnalyzeResponse> response = spaceAnalyzeService.getSpaceUserAnalyze(analyzeRequest, loginUser);
        return ApiResponse.success("获取用户上传行为分析成功！", response);
    }

    @Operation(summary = "获取空间排行情况")
    @PostMapping("/rank")
    @AuthCheck(requiredRole = UserConstant.ADMIN_ROLE)
    public ApiResponse<List<Space>> getSpaceRankAnalyze(@RequestBody SpaceRankAnalyzeRequest analyzeRequest, HttpServletRequest request) {
        ThrowUtils.throwIf(analyzeRequest == null, ResponseCodeEnum.INVALID_PARAMETER);
        User loginUser = userService.getLoginUser(request);
        List<Space> response = spaceAnalyzeService.getSpaceRankAnalyze(analyzeRequest, loginUser);
        return ApiResponse.success("获取空间排行情况成功！", response);
    }
}
